<template>
  <j-modal
    :maskClosable="false"
    :title="title"
    :width="width"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">

        <a-form-item v-if="da.type == 4" label="签约日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date v-decorator="['signingDate', validatorRules.signingDate]"
            :trigger-change="true" style="width: 100%"/>
        </a-form-item>

        <a-form-item v-if="da.type == 1" label="到租时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date :disabled="true"
                  v-decorator="['rentEndTime']"
                  :trigger-change="true" style="width: 100%"/>
        </a-form-item>
        <a-form-item v-if="da.type == 1" label="续约到期日期" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <j-date placeholder="请选择续约到期日期" v-decorator="['endDate', validatorRules.endDate]" :trigger-change="true"
                  style="width: 100%"/>
        </a-form-item>
        <a-form-item v-if="da.type == 1" label="月租金" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['monthlyRent', validatorRules.monthlyRent]" placeholder="请输入月租金"
                          style="width: 100%"/>
        </a-form-item>


        <a-form-item v-if="da.type == 2 || da.type == 3" label="退回租金" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['rent']" placeholder="请输入退回租金" style="width: 100%"/>
        </a-form-item>
        <!--因为保证金不归系统管理，不退保证金-->
        <a-form-item v-if="da.type == 2 || da.type == 3 || da.type == 5" label="退回保证金" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number v-decorator="['cashDeposit']" placeholder="请输入退回保证金" style="width: 100%"/>
        </a-form-item>

        <a-form-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-textarea rows="4" v-decorator="['remark']" placeholder="请输入备注" style="width: 100%"/>
        </a-form-item>

      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>

  import JDate from '@/components/jeecg/JDate'
  import { postTakeEffect, postRenew, postSurrender, postTermination, postTerminate } from '@/api/leaseContract'
  import { dateFormatA } from '@/utils/util'

  export default {
    name: 'LeaseContractOperationModal',
    components: {
      JDate
    },
    data () {
      return {
        form: this.$form.createForm(this),
        title: '合同操作',
        width: 600,
        visible: false,
        model: {},
        da: {
          id: '',
          type: 0
        },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 }
        },
        confirmLoading: false,
        validatorRules: {
          endDate: {
            rules: [
              { required: true, message: '请选择续约到期日期!' }
            ]
          },
          signingDate: {
            rules: [
              { required: true, message: '请选择合同签约日期!' }
            ]
          },
          monthlyRent: {
            rules: [
              { required: true, message: '请输入月租金!' },
              { pattern: /^(?!(0[0-9]{0,}$))[0-9]{1,}[.]{0,}[0-9]{0,}$/, message: '请输入零以上的数字' }
            ]
          }
        }
      }
    },
    created () {

      // this.selectSub()
    },
    methods: {
      show (title, re, type) {
        this.confirmLoading = false
        this.form.resetFields()
        this.da.type = type
        this.da.id = re.id
        this.model = Object.assign({}, re)
        this.title = title
        this.visible = true
        // this.$nextTick(() => {
        //   this.form.setFieldsValue({username:username});
        // });
        this.setDate(re, type)
      },
      setDate (re, type) {

        if (type == 2 || type == 3 || type == 5) {
          this.$nextTick(() => {
            this.form.setFieldsValue({ cashDeposit: re.cashDeposit, rent: 0 })
          })
        } else if (type == 1) {
          let monthlyRent = re.monthlyRent
          if (re.contractChangeRatio != null && re.contractChangeRatio != undefined && re.contractChangeRatio != '') {
            monthlyRent = (parseFloat(re.monthlyRent) * (1 + parseFloat(re.contractChangeRatio))).toFixed(2)
          }
          this.$nextTick(() => {
            this.form.setFieldsValue({ monthlyRent: monthlyRent, rentEndTime: re.rentEndTime })
          })
        } else if (type == 4) {
          let signingDate = re.signingDate
          if (!signingDate) {
            signingDate = dateFormatA()
          }
          this.$nextTick(() => {
            this.form.setFieldsValue({ signingDate: signingDate })
          })
        }
      },
      close () {
        this.$emit('close')
        this.visible = false
      },
      handleOk () {
        const that = this
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true
            let formData = Object.assign(this.model, values)
            formData.id = this.da.id
            let action
            if (this.da.type == 1) {
              action = postRenew(formData)
            } else if (this.da.type == 2) {
              action = postSurrender(formData)
            } else if (this.da.type == 3) {
              action = postTermination(formData)
            } else if (this.da.type == 4) {
              action = postTakeEffect(formData)
            } else if (this.da.type == 5) {
              action = postTerminate(formData)
            } else {
              that.confirmLoading = false
              return
            }
            action.then((res) => {
              if (res.success) {
                this.$message.success(res.message)
                this.$emit('ok')
                this.close()
              } else {
                this.$message.warning(res.message)
              }
            }).finally(() => {
              this.confirmLoading = false
            })
          }

        })
      },
      handleCancel () {
        this.close()
      }

    }
  }
</script>